package com.app.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

//2020-10-11 15:53:45
@RequestMapping(value = "/download")
@RestController("DownloadController")
public class DownloadController {
	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DownloadController.class);

	private static final String BASE_FILE_URL = "/app/file";

	/**
	 * @describe 使用token鉴权的文件下载<br/>
	 *           参考 https://blog.csdn.net/itmm_wang/article/details/107705613<br/>
	 * @param id 文件唯一编码 uuid
	 * @return void
	 */
	@GetMapping("/examcorepaper/**")
	public void downloadExamcorepaper(@RequestParam Map<String, String> ps) throws IOException {
		// 测试路径nginx http://localhost:9090/app/file?id=1&token=test&type=type11
		// 测试路径nginx
		// http://localhost:9090/app/file/examcorepaper/2021-06-04/11.txt?id=1&token=test&type=type11
		// 测试路径 http://localhost:8080/file/download_file?id=1&token=test&type=type11

		System.out.println("ps--->" + ps);
		// 从id获取文件下载路径
		String fileUrl = "";
		if ("1".equals((String) ps.get("id")) && "test".equals((String) ps.get("token"))) {
			fileUrl = "/examcorepaper/2021-06-04/11.txt";
		}
		String fileName = new String("测试文件.txt".getBytes("GBK"), "iso-8859-1");
		fileUrl = BASE_FILE_URL + fileUrl;
		boolean success=false;
		if(success) {
		// 设置下载响应头信息
		  setDownloadHeader(fileUrl, fileName);
		}
		else {
		  //setDownloadHeader("no_right", "no_right");
			throw new RuntimeException("no rigth");
		}
		

	}

	private void setDownloadHeader(String fileUrl, String fileName) throws UnsupportedEncodingException {

		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		// HttpServletRequest request = servletRequestAttributes.getRequest();
		HttpServletResponse response = servletRequestAttributes.getResponse();

		// 已被授权访问
		// 文件下载
		response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
		// 文件以二进制流传输
		response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
		// 返回真实文件路径交由 Nginx 处理，保证前端无法看到真实的文件路径。
		// 这里的 "/app/file" 为 Nginx 中配置的下载服务名
		response.setHeader("X-Accel-Redirect", fileUrl);
		// 限速，单位字节，默认不限
		// response.setHeader("X-Accel-Limit-Rate","1024");
		// 是否使用Nginx缓存，默认yes
		// response.setHeader("X-Accel-Buffering","yes");
		response.setHeader("X-Accel-Charset", "utf-8");

		// 禁止浏览器缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setHeader("Expires", "0");
	}

}